using System;
using System.Collections.Generic;
using System.Text;

namespace GeorgeHerolyants.Converters
{
    internal class ArgumentsParser
    {
        private const string helpOption = "-help";
        private const string dirOption = "-dir";
        private const string inputOption = "-in";
        private const string outputOption = "-out";
        private const string overwriteOption = "-overwrite";
        private const string searchOption = "-search";
        private const string recursiveOption = "-recursive";

        public static IConverter Parse ( string[] args )
        {
            IConverter converter = new FileConverter();
            int currentToken = 0;

            if ( args[ currentToken ] == helpOption )
            {
                return new NullConverter();
            }
            if ( args[ currentToken ] == dirOption )
            {
                converter = new DirectoryConverter();
                currentToken++;
            }

            if ( args[ currentToken ] == inputOption )
            {
                currentToken++;
                converter.InputPath = args[ currentToken ];
                currentToken++;
            }
            else
            {
                throw new ArgumentException( "You must specify input path" );
            }

            if ( args[ currentToken ] == overwriteOption )
            {
                converter.OutputPath = converter.InputPath;
                currentToken++;
            }
            else if ( args[ currentToken ] == outputOption )
            {
                currentToken++;
                converter.OutputPath = args[ currentToken ];
                currentToken++;
            }
            else
            {
                throw new ArgumentException( "You must specify output path" );
            }

            if ( currentToken >= args.Length )
                return converter;
            if ( args[ currentToken ] == searchOption )
            {
                currentToken++;
                converter.SearchPattern = args[ currentToken ];
                currentToken++;
            }

            if ( currentToken >= args.Length )
                return converter;
            if ( args[ currentToken ] == recursiveOption )
            {
                currentToken++;
                converter.SearchRecursive = true;
                currentToken++;
            }

            return converter;
        }
    }
}
